package com.jy.modules.cims.deal.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jy.modules.cims.common.sysUtils.ExceptionUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.common.service.TCMmLoanDetailService;
import com.jy.modules.cims.contract.service.ContractService;
import com.jy.modules.cims.data.common.dto.TCMmLoanDetailDTO;
import com.jy.modules.cims.data.dto.LoanDetailDto;
import com.jy.modules.cims.deal.service.LoanDealService;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.restservice.exception.AbaboonException;
import com.jy.platform.restservice.web.base.BaseController;

/**
 * @classname: LoanDetailController
 * @description: 定义  贷款明细 控制层
 * @author:  zhuwenbin
 */
@Controller
@Scope("prototype")
@RequestMapping("/loanDetail")
public class LoanDetailController extends BaseController{
    private static final Logger logger =  LoggerFactory.getLogger(LoanDetailController.class);

    @Autowired
    @Qualifier("com.jy.modules.cims.component.common.service.TCMmLoanDetailService")
    private TCMmLoanDetailService service;
    @Autowired
    @Qualifier("com.jy.modules.cims.deal.service.LoanDealService")
    private LoanDealService loanDealService;
    
    @Autowired
    @Qualifier("com.jy.modules.cims.contract.service.ContractService")
    private ContractService serviceContract;
    
    /**
     * 通过 Controller 请求 跳转 进入至相关 页面
     */
    @RequestMapping(value = "/prepareExecute/{operate}")
    public ModelAndView execute(@PathVariable("operate") String operate) throws AbaboonException {
        ModelAndView model = new ModelAndView();
        
        if("toQuery".equals(operate)){//跳转至 查询页面
        	model.setViewName("cims/loanDealManage/queryTCMmLoanDetail");
        }else if("toUpdate".equals(operate)){//跳转至贷款交易报盘管理修改页面
        	String id = this.getParameterString("id");
        	model = this.queryOneDTO(id);
        	model.setViewName("cims/loanDealManage/updateTCMmLoanDetail");
        }else if("toBatchUpdate".equals(operate)){
        	String loanStatus = this.getParameterString("loanStatus");
        	model = this.getLoanStatus(loanStatus);
        	model.setViewName("cims/loanDealManage/batchUpdateLoanDetail");
        } 
        
        return model;
    }
    

    
    /**
     * @author zhuwenbin
     * @description:想批量修改页面返回贷款状态
     * @date 2015-03-24 16:21:37
     * @param id
     * @return
     */
    private ModelAndView getLoanStatus(String loanStatus) throws AbaboonException {
        ModelAndView model = new ModelAndView();
        try{
        	TCMmLoanDetailDTO dto = new TCMmLoanDetailDTO();
        	dto.setLoanStatus(loanStatus);
            //将信息放入 model 以便于传至页面 使用
            model.addObject("dto", dto);
        }catch(Exception e){
        	throw new AbaboonException("执行getLoanStatus异常：",e);
        }
        return model;
    }
    

    /**
     * @author zhuwenbin
     * @description:编辑
     * @date 2015-03-26 20:32:17
     * @param request
     * @param dto
     * @param pageData
     * @return
     * @throws
     */
    @RequestMapping(value = "/updateLoanDetail")
    @ResponseBody
    public DataMsg updateLoanDetail(HttpServletRequest request, LoanDetailDto dto, @ModelAttribute DataMsg dataMsg){
        
    	Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);
    	try {
        	loanDealService.updateTCMmLoanDetail(paramMap);
            dataMsg = super.initDataMsg(dataMsg);
            dataMsg.setMsg("修改成功");
        }catch (Exception e) {
        	dataMsg.failed(e.getMessage());
            logger.error("执行方法updateLoanDetail异常：", e);
        }
        return dataMsg;
    }
    
    /**
     * @param request
     * @param dto
     * @param pageData
     * @return
     * @throws
     */
    @RequestMapping(value = "/updateRiskLoanDetail")
    @ResponseBody
    public DataMsg updateRiskLoanDetail(HttpServletRequest request, @ModelAttribute DataMsg dataMsg)throws Exception{
        try {
        	String contractNo = (String) this.getParameterString("contractNo");
        	if(SimpleStringUtils.hasText(contractNo)){
            	loanDealService.updateEmergencyDroppage(contractNo);
        	}else{
        		throw new Exception("合同号不能为空！");
        	}
            dataMsg = super.initDataMsg(dataMsg);
            dataMsg.setMsg("紧急扣回成功！");
            dataMsg.setStatus("ok");
        }catch (Exception e) {
        	e.printStackTrace();
        	dataMsg.failed(e.getMessage());
            logger.error("执行方法updateRiskLoanDetail异常："+ExceptionUtils.getStackTraceAsString(e));
        }
        return dataMsg;
    }
    
    /**
     * @author zhuwenbin
     * @description:编辑
     * @date 2015-03-26 20:32:17
     * @param request
     * @param dto
     * @param pageData
     * @return
     * @throws
     */
    @RequestMapping(value = "/batchupdateLoanDetail")
    @ResponseBody
    public DataMsg batchupdateLoanDetail(HttpServletRequest request, TCMmLoanDetailDTO dto, @ModelAttribute DataMsg dataMsg){
        try {
        	String loanStatus = (String) this.getParameterString("loanStatus");
        	String ids = (String) this.getParameterString("ids");
        	loanDealService.updateLoanStatusByIds(ids,loanStatus);
            dataMsg = super.initDataMsg(dataMsg);
            dataMsg.setMsg("修改成功");
            dataMsg.setData(this.makeJSONData(dto.getId()));
        }catch (Exception e) {
        	dataMsg.failed(e.getMessage());
            logger.error("执行方法updateLoanDetail异常：", e);
        }
        return dataMsg;
    }
    
    

 
    private ModelAndView queryOneDTO(String id) throws AbaboonException {
        ModelAndView model = new ModelAndView();
        try{
        	TCMmLoanDetailDTO dto = service.queryTCMmLoanDetailByPrimaryKey(id);
            //将信息放入 model 以便于传至页面 使用
            model.addObject("dto", dto);
        }catch(Exception e){
        	throw new AbaboonException("执行queryOneDTO异常：",e);
        }
        return model;
    }
}
